package com.xcity.pomelo.response;

import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.combat.CombatUtils;
import com.xcity.game.combat.unit.CombatableBase;
import com.xcity.game.employee.EmployeeQuality;
import com.xcity.game.employee.EmployeeUtils;
import com.xcity.game.npc.NpcTemplate;
import com.xcity.game.skill.SkillUtils;
import com.xcity.pomelo.response.EquipmentBagResponse.Equipment;
import com.xcity.pomelo.response.EquipmentBagResponse.EquipmentSuit;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class EmployeeInfoResponse extends Response {

	private EmployeeInfo obj;

	public EmployeeInfoResponse(int serial, Employee employee) {
		super(serial);
		this.obj = new EmployeeInfo(employee);
	}

	public EmployeeInfo getObj() {
		return obj;
	}

	public void setObj(EmployeeInfo obj) {
		this.obj = obj;
	}
	
	public static class EmployeeSimpleInfo {
		public long id;
		public int templateId;
		public String name;
		public String icon;
		public EmployeeQuality quality;
		public int combatPower;
		public int level;
		@Deprecated
		public int energy, maxEnergy;
		public int qualityLvl;
		public static final EmployeeSimpleInfo EMPTY = new EmployeeSimpleInfo();
		public EmployeeSimpleInfo() {}
		public EmployeeSimpleInfo(Employee employee) {
			this.id = employee.getId();
			this.templateId = employee.getTemplate().getId();
			this.name = employee.getName();
			this.icon = employee.getIcon();
			this.quality = employee.getTemplate().getQuality();
			this.combatPower = employee.getCombatPower();//CombatUtils.calcCombatPower(employee);
			this.level = employee.getLevel();
			this.energy = employee.getEnergy();
			this.maxEnergy = employee.getMaxEnergy();
			this.qualityLvl = employee.getQualityLvl();
		}
		public EmployeeSimpleInfo(NpcTemplate npc) {
			this.id = npc.getId();
			this.templateId = 90224 + (npc.getId() & 3)/*npc.getId() % 4*/; // make a fake employee template id
			this.name = npc.getName();
			this.icon = npc.getIcon();
			this.quality = EmployeeQuality.A;
			this.combatPower = CombatUtils.calcCombatPower((CombatableBase) npc.toCombatabale());
			this.level = npc.getLevel();
			this.energy = 0;
			this.maxEnergy = 0;
			this.qualityLvl = 2;
		}
	}
	
	public static class EmployeeInfo {
		public long id;
		public int templateId;
		public String name;
		public byte sex;
		public String icon;
		public EmployeeQuality quality;
		public int state; // !!!old version!!!
		public int level;
		public int nowExp, maxExp;
//		public int energy, maxEnergy;
		public int combatPower;
		public CombatAttributeResponse attr;
		public List<Equipment> equipments;
		public List<EquipmentSuit> equipSuits;
//		public List<SkillV2ToV1> skills;
//		public int recoverEnergyCD;
		public int qualityLvl; // 品级
		public int starLvl; // 星级
		public int upQualityId; // 升品表id
		public byte trained; // 是否培养过（升级、升星、升品）
		public String buildName;
		public List<SkillV2Info> skills2; // 新版技能（1主动，5被动）
		
		public EmployeeInfo(Employee refer) {
			this.id = refer.getId();
			this.templateId = refer.getTemplate().getId();
			this.name = refer.getTemplate().getName();
			this.sex = refer.getTemplate().getSex();
			this.icon = refer.getTemplate().getIcon();
			this.quality = refer.getTemplate().getQuality();
			this.state = refer.getState().codeOfOldVersion();
			this.level = refer.getLevel();
			this.nowExp = EmployeeUtils.getCurrentExp(refer.getExp(), refer.getTemplate().getQuality());
			this.maxExp = EmployeeUtils.getNextLvlNeedExp(refer.getLevel(), refer.getTemplate().getQuality());
//			this.energy = refer.getEnergy();
//			this.maxEnergy = refer.getMaxEnergy();
			this.combatPower = refer.getCombatPower();//CombatUtils.calcCombatPower(refer);
			this.attr = new CombatAttributeResponse(refer);
			this.equipments = refer.getEquipments();
			this.equipSuits = refer.getEquipSuits();
//			this.skills = Arrays.asList(new SkillV2ToV1(refer.getSkillManager().getActive()));
//			if (refer.getEnergy() <= 0) {
//				Calendar cal = TimeUtils.getTodayZeroTimeCalendar();
//				cal.add(Calendar.DAY_OF_YEAR, 1);
//				int cd = (int) ((cal.getTimeInMillis() - TimeUpdater.getInstance().now()) / 1000);
//				this.recoverEnergyCD = cd > 0 ? cd : 0;
//			}
			this.qualityLvl = refer.getQualityLvl();
			this.starLvl = refer.getStarLvl();
			this.upQualityId = refer.getTemplate().getUpQualityId();
			this.trained = (byte) (refer.isTrained() ? 1 : 0);
			BuildV2 b = refer.getBuilding();
			this.buildName = b != null ? b.getTemplate().getName() : null;
			
			this.skills2 = SkillUtils.toResponse(refer);
		}
	}

}
